<template>
  <div>
    <div class="div1">
      <span>保险公司信息</span>
      <hr class="hr" />
      <FormPanel ref="formPanel" :list="formList" :cols="3" :labelWidth="100" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle" />
    </div>
    <div class="div1">
      <span>返利信息</span>
      <hr class="hr" />
      <FormPanel ref="formPanel1" :list="formList1" :cols="3" :labelWidth="130" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle1" />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button v-if="type !== 'show'" type="primary" @click="saveDialog">{{ btnText }}</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { getInsurationByCode, addInsuration, updateInsurationByCode } from '@/api/repair/repairBusiness/insuranceCompany';

export default {
  name: 'addInsuranceCompany',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    changeHandle: Function,
    insurationCode: String,
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '修 改' };
    this.saveData = {};
    return {
      formList: this.createFormList(),
      formList1: this.createFormList1(),
      formsb: false,
      formsb1: false
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  mounted() {
    console.log(this.type);
  },
  created() {
    this.init();
  },
  methods: {
    async init() {
      if (this.type === 'edit') {
        this.formList.find(x => x.fieldName === 'insuration_code').disabled = true;
      }
      if (this.type === 'edit' || this.type == 'show') {
        const res = await getInsurationByCode(this.insurationCode);
        this.formList.forEach(x => {
          x.initialValue = res.data[x.fieldName];
        });
        this.formList1.forEach(x => {
          x.initialValue = res.data[x.fieldName];
        });
        this.formList = [...this.formList];
        this.formList1 = [...this.formList1];
      } else {
        this.formList1.find(x => x.fieldName === 'oem_tag').initialValue = '10451002';
        this.formList1.find(x => x.fieldName === 'is_valid').initialValue = 10041001;
        // this.formList1[8].initialValue = 10451002;
        // this.formList1[9].initialValue = 10041001;
      }
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '保险公司代码',
          fieldName: 'insuration_code',
          placeholder: '请输入...',
          rules: [{ required: true, message: '请输入保险公司代码', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '保险公司简称',
          fieldName: 'insuration_short_name',
          placeholder: '请输入...',
          rules: [{ required: true, message: '请输入保险公司简称', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '联系人',
          unit: '元',
          fieldName: 'contactor_name',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '保险公司名称',
          fieldName: 'insuration_name',
          placeholder: '请输入...',
          selfCols: 2,
          rules: [{ required: true, message: '请输入保险公司名称', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '联系人电话',
          fieldName: 'contactor_phone',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '地址',
          fieldName: 'address',
          placeholder: '请输入...',
          selfCols: 2
        },
        {
          type: 'INPUT',
          label: '联系人手机',
          fieldName: 'contactor_mobile',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '开户银行',
          fieldName: 'bank',
          placeholder: '请输入...',
          selfCols: 2
        },
        {
          type: 'INPUT',
          label: '邮编',
          fieldName: 'zip_code',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '开户账号',
          fieldName: 'bank_account',
          placeholder: '请输入...',
          selfCols: 2
        },
        {
          type: 'INPUT',
          label: '传真',
          fieldName: 'fax',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '保险中介机构',
          fieldName: 'insuration_medium',
          placeholder: '请输入...',
          selfCols: 2
        },
        {
          type: 'INPUT',
          label: '保险加价率',
          fieldName: 'increase_rate',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '机构级别',
          fieldName: 'insuration_level',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '上级机构',
          fieldName: 'insuration_higher',
          placeholder: '请输入...'
        }
      ];
    },
    createFormList1() {
      return [
        {
          type: 'INPUT_NUMBER',
          label: '电话营销商业险返利点',
          fieldName: 'busi_insurance_rebate_numd',
          max: 10000,
          min: 0,
          precision: 2,
          placeholder: '请输入...'
        },
        {
          type: 'INPUT_NUMBER',
          label: '信贷商业险返利点',
          fieldName: 'busi_insurance_rebate_numx',
          max: 10000,
          min: 0,
          precision: 2,
          placeholder: '请输入...'
        },
        {
          type: 'INPUT_NUMBER',
          label: '电话营销交强险返利点',
          unit: '元',
          fieldName: 'com_insurance_rebate_numd',
          max: 10000,
          min: 0,
          precision: 2,
          placeholder: '请输入...'
        },
        {
          type: 'INPUT_NUMBER',
          label: '信贷交强险返利点',
          fieldName: 'com_insurance_rebate_numx',
          max: 10000,
          min: 0,
          precision: 2,
          placeholder: '请输入...'
        },
        {
          type: 'INPUT_NUMBER',
          label: '传统商业险返利点',
          fieldName: 'busi_insurance_rebate_numc',
          max: 10000,
          min: 0,
          precision: 2,
          placeholder: '请输入...'
        },
        // {
        //   type: 'INPUT_NUMBER',
        //   label: '其他商业险返利点',
        //   fieldName: 'busi_insurance_rebate_num',
        //   max: 10000,
        //   min: 0,
        //   precision: 2,
        //   placeholder: '请输入...'
        // },
        {
          type: 'INPUT_NUMBER',
          label: '传统交强险返利点',
          fieldName: 'com_insurance_rebate_numc',
          max: 10000,
          min: 0,
          precision: 2,
          placeholder: '请输入...'
        },
        // {
        //   type: 'INPUT_NUMBER',
        //   label: '其他交强险返利点',
        //   fieldName: 'com_insurance_rebate_num',
        //   max: 10000,
        //   min: 0,
        //   precision: 2,
        //   placeholder: '请输入...'
        // },
        {
          type: 'SELECT',
          label: '数据来源',
          fieldName: 'oem_tag',
          placeholder: '请输入...',
          disabled: true,
          itemList: this.createDictList('1045')
        },
        {
          type: 'SELECT',
          label: '是否启用',
          fieldName: 'is_valid',
          placeholder: '请输入...',
          itemList: [
            { text: '是', value: 10041001 },
            { text: '否', value: 10041002 }
          ]
        }
      ];
    },
    formChangeHandle(val) {
      Object.assign(this.saveData, val);
      this.formsb = true;
    },
    formChangeHandle1(val) {
      Object.assign(this.saveData, val);
      this.formsb1 = true;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      this.$refs.formPanel1.SUBMIT_FORM();
      if (this.formsb && this.formsb1) {
        let res = {};
        if (this.type === 'add') {
          addInsuration(this.saveData).then(res => {
            if (res.resultCode === 200) {
              this.$message.success('保存成功');
              // this.closeDrawer();
              this.closeDialog('refresh');
              this.changeHandle();
            } else {
              this.$message.error('保存失败');
            }
          });
        }
        if (this.type === 'edit') {
          this.saveData.insuration_code = this.insurationCode;
          updateInsurationByCode(this.saveData).then(res => {
            if (res.resultCode === 200) {
              this.$message.success('保存成功');
              // this.closeDrawer();
              this.closeDialog('refresh');
              this.changeHandle();
            } else {
              this.$message.error('保存失败');
            }
          });
        }

        // if (res.resultCode === 201) {

        //   this.closeDialog('refresh');
        // }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    }
  }
};
</script>

<style lang="less" scoped>
.div1 {
  border: 1px solid @borderColor;
  border-radius: @borderRadius;
  box-shadow: @boxShadow;
  padding: @modulePadding;
  //margin-bottom:2px;
}

.hr {
  border: 0;
  height: 1px;
  background-color: @borderColor;
}
</style>
